package org.example.yongyou;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.gson.JsonObject;

import java.util.HashSet;
import java.util.Map;

import static cn.hutool.http.ContentType.JSON;

public class 部门列表 {

    private static final String accessToken = "07e3c91c3c864891bec8f236a868b873";
//    data.get("gatewayUrl") = https://c2.yonyoucloud.com/iuap-api-gateway
//            data.get("tokenUrl") = https://c2.yonyoucloud.com/iuap-api-auth
    private static final String gatewayUrl = "https://c2.yonyoucloud.com/iuap-api-gateway";
    private static final String tokenUrl = "https://c2.yonyoucloud.com/iuap-api-auth";
    private static final String requestUrl = "/yonbip/digitalModel/basedoc/dept/list";

    public static void main(String[] args) {

        String url = gatewayUrl + requestUrl+"?access_token=" + accessToken;
        String requestBody="{\n" +
                "    \"data\": {\n" +
                "        \"pubts\": [\n" +
                "            \"2021-01-07 15:33:14\"\n" +
                "        ]\n" +
                "    }\n" +
                "}";

        //post请求
        HashSet<String> codes = new HashSet<>();
        String body = HttpUtil.post(url, requestBody);
        JSONArray jsonArray = (JSONArray) new JSONObject(body).get("data");
        for (Object o : jsonArray) {
            JSONObject jsonObject = (JSONObject) o;
            String code = jsonObject.get("code").toString();
            codes.add(code);
        }
        System.out.println(codes);
    }


    public <T> T execute(Object params) {
        if (params instanceof Map) {
            Map paramMap = (Map) params;
            String codeSet = paramMap.get("codeSet").toString();
            //codeSet去掉开头的 [ 和结尾的 ]
            codeSet = codeSet.substring(1, codeSet.length() - 1);
            String[] codes = codeSet.split(",");

            paramMap.put("codeList", codes);

            return (T) paramMap;
        }
        // TODO
        return null;
    }

}
